<%--
  Created by IntelliJ IDEA.
  User: 闫佳宁
  Date: 2024/1/22
  Time: 9:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%
    String baseUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<html>
<head>
    <base href="<%=baseUrl%>">
    <title>修改部门信息</title>
    <link rel="stylesheet" href="layui/css/layui.css">
</head>
<body>
<form class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label">部门名</label>
        <div class="layui-input-inline">
            <input class="layui-input" type="text" name="name" placeholder="请输入部门的名称">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <input type="hidden" name="id" value="${param.id}">
            <button class="layui-btn" lay-submit lay-filter="edit">编辑</button>
        </div>
    </div>
</form>

<script src="layui/layui.js"></script>
<script>

    layui.use(['form'], function () {
        var form = layui.form;
        var $ = layui.$;

        //数据回显  根据ID得到所有信息
        getDept(${param.id});//用EL表达式来获取id的参数


        //获取 指定ID的员工信息
        function getDept(id) {
            $.ajax({
                url: 'dept?action=getById',
                data: {id: id},
                type: 'get',
                dateType: 'json',
                success(result) {
                    if (result.code === 0) {
                        //数据回显
                        $('input[name="name"]').val(result.data.name);
                    } else {
                        layer.msg(result.msg, {icon: 5});
                    }
                },
                error() {
                    layer.msg('请求失败', {icon: 5});
                }
            });
        }

        //表单的提交
        form.on('submit(edit)', function (data) {
            $.ajax({
                url: 'dept?action=edit',
                data: data.field,
                type: 'post',
                dateType: 'json',
                success(result) {
                    if (result.code == 0) {
                        layer.msg('编辑成功', {icon: 6}, function () {
                            parent.layer.closeAll('iframe');

                        })
                    }else {
                        layer.msg(result.msg, {icon: 5})
                    }
                },
                error() {
                    layer.msg('请求失败', {icon: 5});
                }
            });

            //阻止表单自动提交
            return false;
        });
    });
</script>

</body>
</html>

